using System;
using System.Collections.Generic;
using FlexiCommerce.Components;

namespace FlexiCommerce.Collections
{
	/// <summary>
	/// A filter that removes duplicated by keeping track of already added 
	/// items and only matching new items.
	/// </summary>
	public class DuplicateFilter : ItemFilter
	{
		#region Private Fields
		private Dictionary<int, bool> existing = new Dictionary<int, bool>();	
		#endregion

		#region Public Methods
        public override bool Match(PersistenceItem item)
		{
			if (existing.ContainsKey(item.ID))
				return false;

			existing.Add(item.ID, true);
			return true;
		}

		public void Clear()
		{
			existing.Clear();
		} 
		#endregion

		/// <summary>Removes duplicate items.</summary>
		/// <param name="items">The items whose duplicate items should be deleted.</param>
        public static void FilterDuplicates(IList<PersistenceItem> items)
		{
			ItemFilter.Filter(items, new DuplicateFilter());
		}
	}
}